﻿using DLL.NET50.DB.MS.AdviceAndDrug;
using DLL.NET60.DB.MS.Entity.DTO;
using DLL.Standard.Infrastructure.Entity;
using Prism.Events;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Module.AutoRun
{
    public interface IMS_HPScarBar
    {
        bool SP_CanDispenseMedicinesTogether(CMS_AdviceAndDrug adviceAndDrug, int StationID, ref DTO_adi_dic_drug[] currentUsedDrugs);

        string ErrMsg { get; set; }
    }

    public class CMS_HPScanBar
    {
        [Unity.Dependency]
        public IDialogService _dialogService { get; set; }

        private string _errMsg = "";

        public string ErrMsg
        {
            get { return _errMsg; }
            set
            {
                _errMsg = value;
            }
        }

        private IEventAggregator _eventAggregator;

        public CMS_HPScanBar(IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;
        }

        public CMS_HPScanBar()
        {
        }

        private DTO_adi_dic_drug[] _currentUsedDrugs = new DTO_adi_dic_drug[6];

        public bool SP_CanDispenseMedicinesTogether(CMS_AdviceAndDrug adviceAndDrug, int StationID, ref DTO_adi_dic_drug[] currentUsedDrugs)
        {
            _currentUsedDrugs = currentUsedDrugs;

            bool ret = false;

            ret = DrugNameIsSame(adviceAndDrug, StationID);
            if (false == ret)
            {
                return false;
            }

            ret = DrugTypeIsTheSame(adviceAndDrug, StationID);
            if (false == ret)
            {
                return false;
            }
            //ret=OrgPressureIsTheSame(adviceAndDrug, StationID);
            //if (false==ret)
            //{
            //    return false;
            //}
            ret = PressureIsTheSame(adviceAndDrug, StationID);
            if (false == ret)
            {
                return false;
            }
            //ret= FlushGroupIsTheSame(adviceAndDrug, StationID);
            //if (false==ret)
            //{
            //    return false;
            //}
            if (_currentUsedDrugs[StationID].SOLVENT_FLAG == null)
            {
                _currentUsedDrugs[StationID] = adviceAndDrug.Drugs[0];
            }
            return true;
        }

        //第一步判断药品名称有误改变
        private bool DrugNameIsSame(CMS_AdviceAndDrug adviceAndDrug, int StationID)
        {
            string drugName = adviceAndDrug.Drugs[0].DRUG_NAME.Trim();
            //混配模式，每站的药可以不同，但每个站的药不可以改变
            if (_currentUsedDrugs[StationID].SOLVENT_FLAG != null)
            {
                if (_currentUsedDrugs[StationID].DRUG_NAME.Trim() == drugName)
                {
                    //该占药品没有发生改变
                    return true;
                }
                else
                {
                    //该占位药品发生了改变
                    ErrMsg = "该站药品发生改变！记录的药品名称=" + _currentUsedDrugs[StationID].DRUG_NAME + "当前扫描的药品名称=" + drugName;
                    return false;
                }
            }
            return true;
        }

        //第二步，判断，是不是都是水针，或者都是粉针，或者都是溶媒
        public bool DrugTypeIsTheSame(CMS_AdviceAndDrug adviceAndDrug, int StationID)
        {
            //逻辑1：初始类型全为0；2：粉针=1；水针=2；专用溶媒=3；3：后面所有的药都和第一个药做比较
            int ndrugType = GetDrugType(adviceAndDrug);

            {
                //已经有药品类型，判断药品类型是否相同
                for (int i = 0; i < StationID; i++)
                {
                    if (_currentUsedDrugs[i].SOLVENT_FLAG != null)
                    {
                        int odrugType = GetDrugType(_currentUsedDrugs[i]);
                        if (odrugType != ndrugType)
                        {
                            //提示报错
                            string ndrugTypeName = GetDrugTypeNameByID(ndrugType);
                            string odrugTypeName = GetDrugTypeNameByID(odrugType);

                            //MessageBox.Show("正在配药的药品类型是"+odrugTypeName+";当前医嘱的药品类型是"+ndrugTypeName+".无法在一起混配");
                            string str = "正在配药的药品类型是" + odrugTypeName + ";当前医嘱的药品类型是" + ndrugTypeName + ".无法在一起混配";
                            Application.Current.Dispatcher.Invoke(
                            (Action)delegate ()
                            {
                                _dialogService.ShowDialog("WarningDialog", new DialogParameters($"message={str}"), null, GlobalDialogNames.CustomDialogWindow);
                            });
                            return false;
                        }
                    }
                }
            }
            return true;
        }

        private string GetDrugTypeNameByID(int id)
        {
            string name = "";
            switch (id)
            {
                case 1:
                    {
                        name = "粉针";
                        break;
                    }
                case 2:
                    {
                        name = "水针";
                        break;
                    }
                case 3:
                    {
                        name = "专用溶媒";
                        break;
                    }
            }
            return name;
        }

        private int GetDrugType(CMS_AdviceAndDrug adviceAndDrug)
        {
            DTO_adi_dic_drug drug = adviceAndDrug.Drugs[0];
            return GetDrugType(drug);
        }

        private int GetDrugType(DTO_adi_dic_drug drug)
        {
            int drugType = 0;
            if (drug.SOLVENT_FLAG == 0)
            {
                if (drug.NEEDLE_TYPE == 1)
                {
                    //水针
                    drugType = 2;
                }
                else
                {
                    drugType = 1;
                }
            }
            else if (drug.SOLVENT_FLAG == 2)
            {
                //专用溶媒
                drugType = 3;
            }
            return drugType;
        }

        //第三步，判断瓶内气压是否，相同，气压不同不可以一起混配
        private bool OrgPressureIsTheSame(CMS_AdviceAndDrug adviceAndDrug, int StationID)
        {
            DTO_adi_dic_drug scanDrug = adviceAndDrug.Drugs[0];
            //if (_currentUsedDrugs[StationID].SOLVENT_FLAG!=null)
            {
                for (int i = 0; i < StationID; i++)
                {
                    if (_currentUsedDrugs[i].SOLVENT_FLAG != null)
                    {
                        //已经扫描了药，判断压强是否相同
                        if (_currentUsedDrugs[i].ORIGINAL_PRESSURE != scanDrug.ORIGINAL_PRESSURE)
                        {
                            ErrMsg = "药品属性:原始压强不同，不能在一起混配！";
                            return false;
                        }
                    }
                }
            }
            return true;
        }

        private bool PressureIsTheSame(CMS_AdviceAndDrug adviceAndDrug, int StationID)
        {
            DTO_adi_dic_drug scanDrug = adviceAndDrug.Drugs[0];
            //if (_currentUsedDrugs[StationID].SOLVENT_FLAG!=null)
            {
                for (int i = 0; i < StationID; i++)
                {
                    if (_currentUsedDrugs[i].SOLVENT_FLAG != null)
                    {
                        //已经扫描了药，判断压强是否相同
                        if (_currentUsedDrugs[i].POSITIVE_PRESSURE != scanDrug.POSITIVE_PRESSURE)
                        {
                            ErrMsg = "药品属性:冲刷后压强不同，不能在一起混配！";
                            return false;
                        }
                    }
                }
            }
            return true;
        }

        //第四步：循环次数不同，不能在一起混配
        private bool LoopCountIsTheSame(CMS_AdviceAndDrug adviceAndDrug, int StationID)
        {
            int nflushLoopCount = GetFlushLoopCount(adviceAndDrug.Drugs[0]);

            if (_currentUsedDrugs[StationID].SOLVENT_FLAG != null)
            {
                //已经扫描了药，判断压强是否相同
                for (int i = 0; i <= StationID; i++)
                {
                    if (_currentUsedDrugs[i].SOLVENT_FLAG != null)
                    {
                        int oflushLoopCount = GetFlushLoopCount(_currentUsedDrugs[i]);
                        if (oflushLoopCount != nflushLoopCount)
                        {
                            ErrMsg = "药品冲刷循环次数不同，不能在一起混配";
                            Application.Current.Dispatcher.Invoke(
                            (Action)delegate ()
                            {
                                _dialogService.ShowDialog("WarningDialog", new DialogParameters($"message={ErrMsg}"), null);
                                //_dialogService.ShowDialog("SuccessDialog", new DialogParameters($"message={str}"), null);
                            });
                            return false;
                        }
                    }
                }
            }
            return true;
        }

        private int GetFlushLoopCount(DTO_adi_dic_drug drug)
        {
            int flushGroupIndex = drug.FlushGroupIndex;
            int flushLoopCount = drug.flush_Groups[flushGroupIndex].FlushLoopCount;
            return flushLoopCount;
        }

        //第五步：抽一次溶媒配几支药的数量不同，也不能混配

        //判断冲刷属性
        private bool FlushGroupIsTheSame(CMS_AdviceAndDrug adviceAndDrug, int StationID)
        {
            DTO_adi_FlushGroup newFlushGroup = GetFlushGroup(adviceAndDrug.Drugs[0]);

            //if (_currentUsedDrugs[StationID].SOLVENT_FLAG!=null)
            {
                for (int i = 0; i < StationID; i++)
                {
                    if (_currentUsedDrugs[i].SOLVENT_FLAG != null)
                    {
                        DTO_adi_FlushGroup oldFlushGroup = GetFlushGroup(_currentUsedDrugs[i]);
                        bool ret = FlushParamsIsTheSame(oldFlushGroup, newFlushGroup);
                        if (false == ret)
                        {
                            return false;
                        }
                    }
                }
            }
            return true;
        }

        private DTO_adi_FlushGroup GetFlushGroup(DTO_adi_dic_drug drug)
        {
            int flushGroupIndex = drug.FlushGroupIndex;
            DTO_adi_FlushGroup flushGroup = drug.flush_Groups[flushGroupIndex];
            return flushGroup;
        }

        private bool FlushParamsIsTheSame(DTO_adi_FlushGroup oldFlushGroup, DTO_adi_FlushGroup newFlushGroup)
        {
            int OldLoopCount = oldFlushGroup.FlushLoopCount + 1;
            int NewLoopCount = newFlushGroup.FlushLoopCount + 1;
            if (OldLoopCount > NewLoopCount)
            {
                for (int i = 0; i < NewLoopCount; i++)
                {
                    DTO_adi_FlushParam oldFlushParam = oldFlushGroup.flush_Params[i];
                    DTO_adi_FlushParam newFlushParam = newFlushGroup.flush_Params[i];
                    bool same = FlushParamIsTheSame(oldFlushParam, newFlushParam);
                    if (false == same)
                    {
                        return false;
                    }
                }
            }
            else
            {
                for (int i = 0; i < OldLoopCount; i++)
                {
                    DTO_adi_FlushParam oldFlushParam = oldFlushGroup.flush_Params[i];
                    DTO_adi_FlushParam newFlushParam = newFlushGroup.flush_Params[i];
                    bool same = FlushParamIsTheSame(oldFlushParam, newFlushParam);
                    if (false == same)
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        private bool FlushParamIsTheSame(DTO_adi_FlushParam oldFlushParam, DTO_adi_FlushParam newFlushParam)
        {
            return true;
            if (oldFlushParam.FlushMethod != newFlushParam.FlushMethod)
            {
                //高处，低处，运动，混动
                ErrMsg = "冲刷方式不同，无法在一起混配！";
                return false;
            }
            return true;
            if (oldFlushParam.FlushOffsetHeight != newFlushParam.FlushOffsetHeight)
            {
                //冲刷高度补偿不一样
                ErrMsg = "冲刷高度补偿值不相等，无法在一起混配！";
                return false;
            }
            if (oldFlushParam.OutLowOffset != newFlushParam.OutLowOffset)
            {
                //抽液最低位补偿
                ErrMsg = "冲刷抽液低处补偿值不等，无法在一起混配！";

                return false;
            }

            if (oldFlushParam.DrugCountOfLoopFlush != newFlushParam.DrugCountOfLoopFlush)
            {
                //抽液最低位补偿
                ErrMsg = "溶解属性不同，无法在一起混配！";

                return false;
            }
            return true;
        }

        #region 遇到的问题

        /*
         * 1.原始压强为正压，但每个药抽出的空气量不一样，药品数量不一样，如果有的一次可以抽完，有的需要几次抽完，如何解决？
         * 2.药品的冲刷循环字数不同，需要每组的冲刷属性都做判断，
         */

        #endregion 遇到的问题
    }
}